package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.KnowledgeFood;
import com.itheima.service.KnowledgefoodService;
import com.itheima.utils.POIUtils;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/knowledgefood")
public class KnowledgefoodController  {
@Reference
private KnowledgefoodService knowledgefoodService;
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = knowledgefoodService.pageQuery(queryPageBean);
        return pageResult;
    }
    @RequestMapping("/add.do")
    public Result add(@RequestBody KnowledgeFood knowledgeFood) {
        try {
            knowledgefoodService.add(knowledgeFood);
        } catch (Exception e) {


            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_KNOWLEDGEFOOD_FAIL);
        }

        return new Result(true, MessageConstant.ADD_CHECKITEM_KNOWLEDGEFOOD_SUCCESS);
    }
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<KnowledgeFood> data =new ArrayList<>();
            for(String[] strings : list){
                String foodData = strings[0];
                String number = strings[1];
                KnowledgeFood knowledgeFood = new KnowledgeFood(new Date(foodData),Integer.parseInt(number));
                data.add(knowledgeFood);
            }
        } catch (IOException e) {
            e.printStackTrace();
            Result result =new Result(false, MessageConstant.PIC_CHECKITEM_KNOWLEDGEFOOD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_CHECKITEM_KNOWLEDGEFOOD_SUCCESS);
    }
    @RequestMapping("/delete.do")
    public Result delete(Integer id) {
        try {
            knowledgefoodService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }


        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody KnowledgeFood knowledgeFood) {
        try {
            knowledgefoodService.edit(knowledgeFood);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }


        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findById.do")
    public Result findById(Integer id) {
        try {
            KnowledgeFood knowledgeFood  =knowledgefoodService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,knowledgeFood);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


}
